r"""
Time : 2024/5/31 下午2:26
Author : TJC
@FileName: testcases\conftest.py
@Desc: web测试用例的前置
"""

import pytest
import psutil
import allure
from selenium import webdriver
from utils.timer import dt_strftime
from utils.logger import log
from common.read_cfg import web_cfg
from config.config import cm
from selenium.webdriver.chrome.service import Service


web_driver = None


@allure.title("浏览器驱动")
@pytest.fixture(scope='session', autouse=True)
def drivers(request):

    global web_driver

    # 创建 Service 对象，调用WebDriver驱动程序
    service = Service(cm.web_driver)

    if web_driver is None:
        option = webdriver.ChromeOptions()
        option.binary_location = r"C:\Program Files\Google\Chrome\Application\chrome.exe"  # 根据实际目录修改,自己的chrome浏览器路径
        # option.binary_location = r'C:\Program Files\Google\Chrome\Application\chrome.exe'  # 根据实际目录修改,自己的chrome浏览器路径
        web_driver = webdriver.Chrome(service=service,options=option)

    @allure.step("退出浏览器驱动")
    def fn():
        web_driver.quit()

    @pytest.fixture(scope='function', autouse=True)
    def close_quit(self):
        process_name = web_cfg.web_driver_process
        # 查找所有的Chrome驱动进程
        process_list = [process for process in psutil.process_iter() if process.name() == process_name]
        if len(process_list) > 0:
            # 如果有多个Chrome驱动程序正在运行，则杀死所有的Chrome驱动程序
            for process in process_list:
                process.kill()
            log.info('存在Chrome驱动程序，并且已杀死所有Chrome驱动程序')
        else:
            log.info('没有Chrome驱动程序正在运行')

    @pytest.fixture(scope='function', autouse=True)
    def reconnect(self):
        process_name = web_cfg.web_driver_process
        # 查找所有的Chrome驱动进程
        process_list = [process for process in psutil.process_iter() if process.name() == process_name]
        if len(process_list) > 0:
            # 如果有多个Chrome驱动程序正在运行，则杀死所有的Chrome驱动程序
            if process_list[0] != process_list[1]:
                fn()
        else:
            log.info('没有Chrome驱动发生重启')

    request.addfinalizer(fn)
    with allure.step("返回浏览器驱动"):
        return web_driver


@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
    # 后置，获取测试结果
    outcome = yield
    reps = outcome.get_result()

    if reps.when == 'call' and reps.failed:
        # 在测试失败时进行截图, 添加到allure报告中
        img = web_driver.get_screenshot_as_png()
        # 为截图文件命名
        name = '_'.join([reps.nodeid.replace('testcases/', '').replace('::', '_'), dt_strftime('%Y%m%d %H%M%S')])
        allure.attach(img, name=name, attachment_type=allure.attachment_type.PNG)


@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_call(item):
    # 记录正在运行的用例
    called = item.nodeid.replace('testcases/', '').replace('::', '-')
    log.info('case：%s', called)
    yield

